package cn.isjinhao.se.sql.grammar.statement.update.visitor;

import cn.isjinhao.se.sql.grammar.statement.StatementAstMetaInfoDetectorDelegate;
import cn.isjinhao.se.sql.grammar.statement.update.SingleUpdateMetaInfo;
import cn.isjinhao.se.sql.grammar.statement.update.represention.Update;
import cn.isjinhao.se.sql.lexical.token.Token;

import java.util.Set;

/**
 * @author 01395265
 * @date 2021/4/6
 */
public class SingleUpdateMetaInfoDetector extends UpdateVisitorWithDelegate<Set<Token>> {

    private SingleUpdateMetaInfo singleUpdateMetaInfo;

    public SingleUpdateMetaInfoDetector() {
        super.init(new StatementAstMetaInfoDetectorDelegate(this));
    }

    @Override
    public Set<Token> visitUpdate(Update update) {
        init(update);

        Token tableName = update.getTableName();
        singleUpdateMetaInfo.addReferenceTable(tableName);

        Set<Token> assignmentList = update.getAssignmentList().accept(this);
        singleUpdateMetaInfo.addReferenceColumnSet(assignmentList);
        Set<Token> whereSeg = update.getWhereSeg().accept(this);
        singleUpdateMetaInfo.addReferenceColumnSet(whereSeg);

        return singleUpdateMetaInfo.getReferenceColumnSet();
    }


    private void init(Update update) {
        singleUpdateMetaInfo = new SingleUpdateMetaInfo(update);
        update.setSingleUpdateMetaInfo(singleUpdateMetaInfo);
    }

}
